package io.adrainty.gbatis.core;

import io.adrainty.gbatis.entity.MapperStatement;
import io.adrainty.gbatis.exception.CoreException;
import io.adrainty.gbatis.transation.Transaction;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.sql.SQLException;
import java.util.Map;

/**
 * @author adrainty
 * @version v1.0.0
 * @since 2024/1/14 22:16
 */

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SqlSessionFactory {

    /**
     * 事物管理器
     */
    private Transaction transaction;

    /**
     * 封装SQL语句的Map集合
     * Key是SQLId
     * Value是对应的SQL标签信息对象
     */
    private Map<String, MapperStatement> mapperStatements;

    /**
     * 获取SQL会话对象
     */
    public SqlSession openSession() {
        try {
            transaction.openConnection();
        } catch (SQLException e) {
            throw new CoreException(e.getMessage());
        }
        return new SqlSession(this);
    }

}
